package 对撞指针;

import java.util.HashSet;
import java.util.Set;

public class LeetCode345_反转字符串中的元音字母 {
    public static String reverseVowels(String s) {
        char[] ch = s.toCharArray();
        Set<Character> o = new HashSet<Character>();
        o.add('a');
        o.add('e');
        o.add('i');
        o.add('o');
        o.add('u');
        o.add('A');
        o.add('E');
        o.add('I');
        o.add('O');
        o.add('U');
        int i = 0;
        int j = s.length()-1;
        while(i < j) {
            if(o.contains(ch[i])) {
                if (o.contains(ch[j])) {
                    swap(ch,i,j);
                    i++;
                    j--;
                }else {
                    j--;
                }
            }else if(o.contains(ch[j])) {
                i++;
            }else{
                i++;
                j--;
            }
        }
        StringBuffer sb = new StringBuffer();
        for (int k = 0; k < ch.length; k++) {
            sb.append(ch[k]);
        }
        return sb.toString();
    }
    private static void swap(char[] s,int i,int j) {
        char ch = s[i];
        s[i] = s[j];
        s[j] = ch;
    }

    public static void main(String[] args) {
        String s = "hello";
        String str = reverseVowels(s);
        System.out.println(str);
    }
}
